package com.wanmait.mpdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.mpdemo.entity.Question;
import com.wanmait.mpdemo.mapper.QuestionMapper;
import com.wanmait.mpdemo.mapper.QuestionTypeMapper;
import com.wanmait.mpdemo.mapper.UserinfoMapper;
import com.wanmait.mpdemo.service.QuestionService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-01-14
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private QuestionTypeMapper questionTypeMapper;

    @Override
    public Question getById(Serializable id) {
        Question question = questionMapper.selectById(id);

        question.setUserinfo(userinfoMapper.selectById(question.getUserinfoId()));
        question.setQuestionType(questionTypeMapper.selectById(question.getQuestionTypeId()));
        return question;
    }

    //Map<Integer,QuestionType> questionTypeMap=...
    @Override
    public List<Question> findAll() {
        //return questionMapper.list();
        List<Question> questions = this.list();
        questions.forEach(question -> {
            question.setQuestionType(questionTypeMapper.selectById(question.getQuestionTypeId()));
            question.setUserinfo(userinfoMapper.selectById(question.getUserinfoId()));
        });
        return questions;
    }

/*    @Override
    public List<Question> findAll(String title) {
        //查询构造器
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        //注意title为列名
        queryWrapper.like("title",title);
        //queryWrapper.gt("id",5);
        List<Question> questions = this.list(queryWrapper);
        return questions;
    }*/
    @Override
    public List<Question> findAll(String title) {
        //查询构造器
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        //QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        //queryWrapper.select("id","title");
        //使用的是类的属性名而不是数据库表的列名
        //queryWrapper.lambda().like(Question::getTitle,title);
        //LambdaQueryWrapper<Question> queryWrapper = Wrappers.lambdaQuery();
        //if(title!=null && !title.equals(""))
        //org.springframework.util.StringUtils
        //if(StringUtils.hasText(title))
       /* Integer hitCount=null;
        if(StringUtils.isNotEmpty(title))
        {
            queryWrapper.like(Question::getTitle,title);
        }
        if(hitCount!=null){
            queryWrapper.gt(Question::getHitCount,hitCount);
        }*/

        //指定查询哪些列，如果不指定则默认查询所有列
        //SELECT id,title FROM
        //queryWrapper.select(Question::getId,Question::getTitle);
        //SELECT title FROM
        //queryWrapper.select(Question::getTitle);

        //指定只查询title列，但是会同时查询主键列，  SELECT id,title FROM question。。。
        //queryWrapper.select(Question.class,tableFieldInfo -> tableFieldInfo.getColumn().equals("title"));
        //注意tableFieldInfo.getColumn()指定的为列名，tableFieldInfo.getProperty()为属性名
        //SELECT id,title,input_time FROM question
         queryWrapper.select(Question.class,tableFieldInfo ->
                tableFieldInfo.getProperty().equals("title")||tableFieldInfo.getProperty().equals("inputTime"));
        //排除info列和has_pic列之外，查询其它所有列
        //queryWrapper.select(Question.class,tableFieldInfo ->
        //        !tableFieldInfo.getColumn().equals("info")&&!tableFieldInfo.getColumn().equals("has_pic"));


        //第一个参数为true则加上该条件，为false不加该条件
        queryWrapper.like(StringUtils.hasText(title),Question::getTitle,title);
        //queryWrapper.gt(hitCount!=null,Question::getHitCount,hitCount);

        List<Question> questions = this.list(queryWrapper);
        return questions;
    }

    @Override
    public List<Question> findAll(Question question) {
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>(question);

        return this.list(queryWrapper);
    }

    @Override
    public Page<Question> findAll(int pageNum, int pageSize) {
        Page<Question> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Question::getId);
        return questionMapper.selectPage(page,queryWrapper);
    }

    @Override
    public PageInfo<Question> findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Question> questions = this.list();
        PageInfo<Question> pageInfo = new PageInfo<>(questions,11);
        return pageInfo;
    }

    @Override
    public List<Question> listByTitle(String title) {
        LambdaQueryWrapper<Question> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(title),Question::getTitle,title);
        List<Question> questions =  this.list(queryWrapper);
        questions.forEach(question -> {
            question.setQuestionType(questionTypeMapper.selectById(question.getQuestionTypeId()));
        });
        return questions;
    }
}
